#include"Protocol.hpp"
#include"Sock.hpp"
static void Usage(string proc)
{
    std::cout<<"Usage: "<<proc<<"proc"<<"server_ip server-port"<<std::endl;
    exit(1);
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
    }
    int sock=Sock::Socket();
    Sock::Connect(sock,argv[1],atoi(argv[2]));
    // while(true)
    // {
    //业务逻辑
    request_t req;
    memset(&req,0,sizeof(req));
    cout<<"Please Enter Data One#";
    cin>>req.x;
    cout<<"Please Enter Data Two#";
    cin>>req.y;
    cout<<"Please Enter operator#";
    cin>>req.op;
    std::string json_string=SerializeRequest(req);
    //ssize_t s=write(sock,&req,sizeof(req));
    ssize_t s=write(sock,json_string.c_str(),json_string.size());
    char buffer[1024];
    s=read(sock,buffer,sizeof(buffer)-1);
    if(s>0)
    {
        response_t resp;
        buffer[s]=0;
        std::string str=buffer;
        DeserializeResponse(str,resp);
        cout<<"code[0:sucess]:"<<resp.code<<endl;
        cout<<"result:"<<resp.result<<endl;
    }
    // if(s<=0)break;
    // response_t resp;
    // s=read(sock,&resp,sizeof(resp));
    // if(s==sizeof(resp))
    // {
    //     cout<<"code[0:sucess]:"<<resp.code<<endl;
    //     cout<<"result:"<<resp.result<<std::endl;
    // }
    // else{
  
    // }
// }
    return 0;
}